<?php
class order_model_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // switch to unit test database from production database
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);

        $this->load->model('order_model');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        // create, check against how many row are inserted in order and order_food table.
        // (food_id, type, en_name, ch_name, quantity, en_size, ch_size, price, sequence)
        $order_food_one = array(
            array('food_id' => '12', 'type' => 'food', 'en_name' => 'chicken brocolli', 'ch_name' => '芥兰鸡',
                'quantity' => '3', 'en_size' => 'small', 'ch_size' => '小', 'price' => '4.50', 'sequence' => '1'),
            array('food_id' => '13', 'type' => 'food', 'en_name' => 'general tso chicken', 'ch_name' => '左宗鸡',
                'quantity' => '1', 'en_size' => 'one size', 'ch_size' => '统一大小', 'price' => '10.50', 'sequence' => '2')
        );
        // type, discount_rate, tax_rate, discount_amount, subtotal, tax, total, date, time, phone, name, address,
        // city, state, zipcode, store_note, customer_note
        // array as a reference
        $order_info = array(
            'type' => 'takeout', 'discount_rate' => '0', 'tax_rate' => '8.75', 'discount_amount' => '0',
            'subtotal' => '10.50', 'tax' => '1.2', 'total' => '11.70', 'date' => '2012-01-11', 'time' => '11:22:34',
            'phone' => '347-888-2232', 'name' => 'david', 'address' => '435 42nd street', 'city' => 'brooklyn',
            'state' => 'new york', 'zipcode' => '11220', 'store_note' => 'make it quick', 'customer_note' => ''
        );

        $create_test1 = $this->order_model->create('takeout', '0', '8.75', '0', '10.50', '1.2', '11.70', '2012-01-11', '11:22:34',
            '347-888-2232', 'david', '435 53rd street', 'brooklyn', 'new york', '11220', 'make it quick', '', $order_food_one);

        // sequence mixture on creation test two
        $order_food_two = array(
            array('food_id' => '12', 'type' => 'food', 'en_name' => 'chicken brocolli', 'ch_name' => '芥兰鸡',
                'quantity' => '3', 'en_size' => 'small', 'ch_size' => '小', 'price' => '4.50', 'sequence' => '2'),
            array('food_id' => '12', 'type' => 'option', 'en_name' => 'extra spicy', 'ch_name' => '加辣',
                'quantity' => '1', 'en_size' => '->', 'ch_size' => '->', 'price' => '0.5', 'sequence' => '3'),
            array('food_id' => '13', 'type' => 'food', 'en_name' => 'general tso chicken', 'ch_name' => '左宗鸡',
                'quantity' => '1', 'en_size' => 'one size', 'ch_size' => '统一大小', 'price' => '10.50', 'sequence' => '1')
        );

        $create_test2 = $this->order_model->create('delivery', '10', '8.75', '0.8', '20.00', '2.4', '21.60', '2012-01-17', '15:23:14',
            '718-288-3231', '', '553 56th street', 'brooklyn', 'new york', '11220', 'extra plate', 'extra sauce', $order_food_two);

        $this->unit->run(count($create_test1['user']), 0, "creation test 1");
        $this->unit->run(count($create_test2['user']), 0, "creation test 2");

        // reading test
        $read_one = $this->order_model->read(1);
        $read_two = $this->order_model->read(2);
        $this->unit->run(count($read_one['food']), 2, "read test 1");
        $this->unit->run(count($read_two['food']), 3, "read test 2");

        // update test
        $order_food_two = array(
            array('food_id' => '12', 'type' => 'food', 'en_name' => 'chicken brocolli', 'ch_name' => '芥兰鸡',
                'quantity' => '3', 'en_size' => 'large', 'ch_size' => '大', 'price' => '8.50', 'sequence' => '1'),
            array('food_id' => '12', 'type' => 'option', 'en_name' => 'extra spicy', 'ch_name' => '加辣',
                'quantity' => '1', 'en_size' => '->', 'ch_size' => '->', 'price' => '0.5', 'sequence' => '2'),
            array('food_id' => '13', 'type' => 'food', 'en_name' => 'general tso chicken', 'ch_name' => '左宗鸡',
                'quantity' => '2', 'en_size' => 'one size', 'ch_size' => '统一大小', 'price' => '10.50', 'sequence' => '3')
        );
        $create_test2 = $this->order_model->update('delivery', '20', '8.75', '3.8', '40.00', '6.4', '51.60', '2012-01-17', '19:23:14',
            '718-288-3231', '', '553 56th street', 'brooklyn', 'new york', '11220', 'extra plate', 'extra sauce', $order_food_two, '2');
        $read_two = $this->order_model->read(2);
        $update_info = $read_two['info'][0];
        $update_food = $read_two['food'];
        $this->unit->run($update_info['time'], '19:23:14', 'update test verification 1');
        $this->unit->run($update_food[0]['en_size'], 'large', 'update test verification 2');


        // delete test
        $this->order_model->delete(1);
        $read_one = $this->order_model->read(1);
        $this->unit->run(count($read_one['food']), 0, "delete test 1");

        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }
}